<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">

    <title>Wish wall</title>
    <link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="../wishwall/css/main.css">
    <!--[if lt IE 9]>
      <script src="lib/html5shiv/html5shiv.min.js"></script>
      <script src="lib/respond/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <!-- header开始 -->
     <%include header.ejs%>
    <!-- header结束 -->

    <!-- intro开始 -->
    <section id="intro">

<%if(!login){%>
     <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <h1>
                    欢迎！
                </h1>

                <p>欢迎来到心愿墙，欢迎注册！</p>

                <p><a class="btn btn-primary btn-lg" href="/regist" role="button">注册</a></p>
            </div>
            <div class="col-lg-6">
                <p>已有账号登陆</p>

                <form>

                <div class="form-group form-group-lg">
                  <label for="useremail">Email address</label>
                  <input type="email" class="form-control" id="useremail" placeholder="Email"></div>
                <div class="form-group form-group-lg">
                  <label for="userpassword">Password</label>
                  <input type="password" class="form-control" id="userpassword" placeholder="Password"></div>

                <button id="denglu" type="button" class="btn btn-success btn-lg">登录</button>
                <div id="cuowukuang" class="alert alert-warning" role="alert">用户名或邮箱被占用</div>
              </form>
                <div class="alert alert-danger" role="alert" id="cuowukuang" style="display:none;"></div>
            </div>
        </div>
    </div>
     <%}else{%>
     <div class="container-fluid">
       <div class="row">

         <div class="col-md-4">
           <div id="username"><%=username%></div>
           <div><textarea name="content" id="content" cols="58" rows="6"></textarea></div>
           <button type="button" id="fabiao" class="btn btn-success pull-right">发表心愿</button>
         </div>
       </div>
     </div>
     <%}%>
    </section>
    <!-- intro结束 -->
    <section id="quanbuwish" class="clearfix">

    </section>
<div class="container">
  <nav>
    <ul class="pagination">

    </ul>
  </nav>
</div>

    <!-- footer开始 -->
    <footer id="footer">

    </footer>
    <!-- footer结束 -->

    <script src="lib/jquery/jquery.js"></script>
    <script type="text/javascript">
  $.get("wishamount",function(result){
    var amount = parseInt(result);
    pageamout = Math.ceil(amount/20);
    for (var i = 0; i < pageamout; i++) {
      $(".pagination").append("<li><a href='javascript:viod(0);'>" + i + "</a></li>");
    }
    $(".pagination li:first").addClass("active");
    $(".pagination li").click(function(){
      var page = $(this).index();
      getPage(page);
      $(this).addClass("active").siblings().removeClass("active")
    })
  });
</script>

    <script src="lib/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../js/underscore-noflect.js"></script>
<script type="text/template" id="moban">
    <div class="col-md-4 grid">
        <h2><img width="40" class="avatar" src="/avatar/{{=avatar}}" alt=""/>{{=username}}说：</h2>

        <p>{{=content}}</p>

        <p>{{=datetime}}</p>

        <p><a class="btn btn-default" href="#" role="button">查看详细</a></p>
    </div>
</script>
    <script type="text/javascript">
    var $quanbuwish = $("#quanbuwish");
    var compiled = _.template($("#moban").html());
    getPage(0);
    function getPage(page){
      $("#quanbuwish").html("");
      $.ajax({
      "url":"/getallwish?page=" + page,
      "type":"get",
      "success":function(result){
        iterator(0);
            function iterator(i){
          if(i==result.length){

            return;
          }
          $.ajax({
            url:"/getuserinfo?username="+result[i].username,
            "type":"get",
            "success":function(result2){
                  result[i].avatar = result2.avatar
var htmlstring = compiled(result[i]);
            $quanbuwish.append($(htmlstring));

                   iterator(i + 1);
            }
          });
        }
      }
    });
    }


    /*$.ajax({
      "url":"/getallwish?page=0",
      "type":"get",
      "success":function(){
        for (var i = 0; i < result.length; i++) {*/
         /* Array.isArray(result);*/
        /*$.ajax(
            {
              url:"/getuserinfo?username="+result[i].username,
              "type":"get",
              "async":false,
             "success":function(result2){
              result[i].avatar = result2.avatar
               var htmlstring = compiled(result.r[i]);
               $quanbuwish.append($(htmlstring));
            }
          });

        }
     }

  });*/


      $("input").focus(function(){
        $("#cuowukuang").fadeOut();
      });
      $("#denglu").click(function(){
        var a = $("#useremail").val();

        $.post("/dologin",{
        "useremail" : $("#useremail").val(),
        "userpassword" : $("#userpassword").val()
      },function(result){
        if(result=="1"){
          //登录成功
          window.location = "/heart";
        }else if(result=="-1"){
          //没有该用户名
          $("#cuowukuang").fadeIn();
          $("#cuowukuang").html("该邮箱未注册");
        }else if(result=="-2"){
          $("#cuowukuang").fadeIn();
          $("#cuowukuang").html("密码错误");

        }else if(result == "-5"){
          //服务器错误
        }
      });
      });


      $("#fabiao").click(function(){

        $.post("/post",{
        "content" : $("#content").val(),

      },function(result){
        if(result=="1"){
          //登录成功
          alert("发表成功");
          window.location = "/heart";

        }else{
          //服务器错误
          alert("发表失败，请联系管理员")
        }
      });
      });


    </script>
  </body>
</html>